package com.xiaobias.day003;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author <a href="mailto:197772517@qq.com">longw.dr</a>
 * @date 2023-11-09 09-37
 * @since 1.0
 */
public class StreamToMap {

    static List<Working> workings = Stream.of(
            Working.builder().invoicePage(1).build(),
            Working.builder().invoicePage(2).build(),
            Working.builder().invoicePage(3).build(),
            Working.builder().invoicePage(4).build(),
            Working.builder().invoicePage(5).build(),
            Working.builder().invoicePage(6).build(),
            Working.builder().invoicePage(7).build(),
            Working.builder().invoicePage(8).build(),
            Working.builder().invoicePage(9).build()
    ).collect(Collectors.toList());

    static List<BusinessSceneDetail> sceneDetails = Stream.of(
            BusinessSceneDetail.builder().dataSourceCode(1).businessSceneCode(1).build(),
            BusinessSceneDetail.builder().dataSourceCode(2).businessSceneCode(2).build(),
            BusinessSceneDetail.builder().dataSourceCode(3).businessSceneCode(3).build(),
            BusinessSceneDetail.builder().dataSourceCode(4).businessSceneCode(4).build(),
            BusinessSceneDetail.builder().dataSourceCode(5).businessSceneCode(5).build(),
            BusinessSceneDetail.builder().dataSourceCode(6).businessSceneCode(6).build(),
            BusinessSceneDetail.builder().dataSourceCode(7).businessSceneCode(7).build(),
            BusinessSceneDetail.builder().dataSourceCode(8).businessSceneCode(8).build(),
            BusinessSceneDetail.builder().dataSourceCode(9).businessSceneCode(9).build()
    ).collect(Collectors.toList());

    static List<SysDictionary> sysDictionaries = Stream.of(
            SysDictionary.builder().sysCode("1").build(),
            SysDictionary.builder().sysCode("2").build(),
            SysDictionary.builder().sysCode("3").build(),
            SysDictionary.builder().sysCode("4").build(),
            SysDictionary.builder().sysCode("5").build(),
            SysDictionary.builder().sysCode("6").build(),
            SysDictionary.builder().sysCode("7").build(),
            SysDictionary.builder().sysCode("8").build(),
            SysDictionary.builder().sysCode("9").build(),
            SysDictionary.builder().sysCode("10").build()
    ).collect(Collectors.toList());

    public static void main(String[] args) {
        // 1、map 对象本身，重复的key，放入List。
        Map<Integer, List<Working>> map = workings.stream().collect(Collectors.toMap(Working::getInvoicePage, e -> {
            List<Working> list = new ArrayList<>();
            list.add(e);
            return list;
        }, (oldList, newList) -> {
            oldList.addAll(newList);
            return oldList;
        }));
        // 或者使用groupBy存为List
        Map<Integer, List<BusinessSceneDetail>> groupByMap = sceneDetails.stream().collect(Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode));
        // 存为set
        Map<Integer, Set<BusinessSceneDetail>> groupBySet = sceneDetails.stream().collect(Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode,
                Collectors.toSet()));
        // 多层map
        Map<Integer, Map<Integer, Set<BusinessSceneDetail>>> mulitMapByGroup = sceneDetails.stream().collect(Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode,
                Collectors.groupingBy(BusinessSceneDetail::getBusinessSceneCode, Collectors.toSet())));
        // 对象子属性 map
        Map<Integer, Set<String>> groupByMapping = sceneDetails.stream().collect(Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode,
                Collectors.mapping(BusinessSceneDetail::getRuleContent, Collectors.toSet())));
        // 对象按照规则获取一个
        Map<Integer, BusinessSceneDetail> collect = sceneDetails.stream().collect(Collectors.toMap(BusinessSceneDetail::getDataSourceCode, Function.identity(), BinaryOperator.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId))));
        // 对象按照规则获取一个里面的值 p肯定存在
        Map<Integer, String> collectingAndThen = sceneDetails.stream().collect(Collectors.groupingBy(BusinessSceneDetail::getDataSourceCode,
                Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(BusinessSceneDetail::getCreatorId)), p -> p.get().getRuleContent())));
        // map的value进行排序
        Map<String, List<SysDictionary>> valueSort = sysDictionaries.stream().collect(
                Collectors.groupingBy(
                        SysDictionary::getSysCode,
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>((Comparator<Object>) (o1, o2) -> 0)),
                                ArrayList::new
                        )
                )
        );
        // map的key进行排序
        Map<String, List<SysDictionary>> keySort = sysDictionaries.stream().collect( Collectors.groupingBy(
                        SysDictionary::getSysCode,
                        TreeMap::new,
                        Collectors.toList()
                ));
    }

}
